<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body style="height: 3000px;">
  <div id="app">
    <button @click='showHide'>{{isShow?'消失':'显示'}}</button>
    <common-head v-if='isShow'></common-head>
  </div>
</body>
</body>
<script src="./vue.js"></script>
<script>
  Vue.component('CommonHead',{
    template: `
      <div>
        <h1 ref='h1'>我是头部组件</h1>
        {{ num }}
      </div>  
    `,
    data() {
      return {
        num: 0,
        timer:null
      }
    },
    created() {
      this.timer = setInterval(()=>{
        this.num++;
        console.log(this.num);
      },1000)
    },
    mounted() {
      window.onscroll = ()=>{
        let sTop = document.documentElement.scrolTop || document.body.scrollTop;
        console.log(sTop);
      }
    },
    beforeDestroy() {
      clearInterval(this.timer)
    },
    destroyed() {
      console.log('destroyed触发了');
    },
  })
  const vm = new Vue({
    el: '#app',
    data: {
      isShow: true
    },
    methods: {
      showHide(){
        this.isShow = !this.isShow
      }
    }
  })
</script>
</html>